<!-- 新建弹出框 -->
<template>
  <div class="add-model">
    <el-dialog
      v-model="dialogVisible"
      :title="title"
      width="30%"
      :before-close="handleClose"
      :rules="rules"
    >
      <el-form :model="form" ref="formRef" label-width="80px">
        <el-form-item label="部门名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="部门领导" prop="leader">
          <el-input v-model="form.leader"></el-input>
        </el-form-item>

        <el-form-item label="上级领导">
          <el-select v-model="form.parentId" style="width: 100%">
            <el-option
              v-for="item in entireDepartments"
              :key="item.id"
              :label="item.name"
              :value="item.parentId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">Cancel</el-button>
          <el-button type="primary" @click="handleConfirmClick"> Confirm </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import type { FormRules } from 'element-plus'
import { ref, reactive } from 'vue'
import useMainStore from '@/store/main/main'
import useSystemStore from '@/store/main/system/system'
import { storeToRefs } from 'pinia'
import { editPageListData } from '@/service/main/system/system'
interface IModelProps {
  modalConfig: {
    pageName: string
    header: {
      addTitle: string
      editTitle: string
    }
    formItems: any[]
  }
}
const props = defineProps<IModelProps>()
const form = reactive<any>({
  name: '',
  leader: '',
  parentId: ''
})
const isPassword = ref(true)
const rules = reactive<FormRules>({
  realname: [{ required: true, message: '请输入真实姓名', trigger: 'blur' }]
})
// 弹窗显示语隐藏
const dialogVisible = ref(false)
const title = ref('新增')
const pageId = ref<number>()
function open(isEdit: Boolean, row?: any): any {
  dialogVisible.value = true
  title.value = isEdit ? '编辑' : '新增'
  pageId.value = isEdit ? row.id : null

  if (isEdit) {
    isPassword.value = false
    for (const key in row) {
      form[key] = row[key]
    }
  } else {
    isPassword.value = true
    for (const key in row) {
      form[key] = ''
    }
  }
}
const formRef = ref()
// 关闭前的回调
function handleClose() {
  dialogVisible.value = false
  for (const key in form) {
    form[key] = ''
  }
}
// 拿到部门和角色的下拉框列表
const mainStore = useMainStore()
const { entireDepartments } = storeToRefs(mainStore)
console.log(entireDepartments)

// 新增人员 先拿到system的pinia里面的方法
const systemStore = useSystemStore()
async function handleConfirmClick() {
  dialogVisible.value = false
  if (isPassword.value) {
    // 有密码框位新建
    systemStore.addPageListAction('department', form)
  } else {
    const id = pageId.value
    await editPageListData('department', id, form)
  }
}
defineExpose({ open })
</script>

<style lang="less" scoped>
.add-model {
  color: red;
}
</style>
